﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.DepartmentModules
{
    public class DepartmentUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<DepartmentModel> GetDepartmentUpdater()
        {
            NormalDataUpdater<DepartmentModel> systemDataUpdater = new NormalDataUpdater<DepartmentModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataDeleting += DepartmentExist;
            return systemDataUpdater;
        }

        private void DepartmentExist(object sender, DataValidationEventArgs<DepartmentModel> e)
        {
            DepartmentFetcher departmentFetcher = new DepartmentFetcher();
            if(departmentFetcher.ListSubDepartment(e.Delete.Site,e.Delete.AutoId,out string message, out rows).Count>0)
            {
                e.Message = "该部门存在子部门，不允许删除";
                e.Cancle = true;
                return;
            }
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            if(employeeFetcher.ListEmployeeByDepartment(e.Delete.Site,e.Delete.AutoId,out message, out rows).Count>0)
            {
                e.Message = "该部门存在人员信息，不允许删除";
                e.Cancle = true;
                return;
            }
        }

        private void SingletonValid(object sender, DataValidationEventArgs<DepartmentModel> e)
        {
            DepartmentFetcher departmentFetcher = new DepartmentFetcher();
            string message = "";
            DepartmentModel department = departmentFetcher.GetByCode(  Authority.Authority.GetUserProfile().SiteId , e.Insert.Code, out message);
            if (department != null && department.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被[{1}]占用", department.Code, department.Name);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<DepartmentModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "部门代号不能为空";
                e.Cancle = true;
                return;
            }
            if (String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "部门名称不能为空";
                e.Cancle = true;
                return;
            }
        }
        
        private void RootUniqueValid(object sender, DataValidationEventArgs<DepartmentModel> e)
        {
            //暂未实现
            DepartmentFetcher departmentFetcher = new DepartmentFetcher();
            string message = "";
            IList<DepartmentModel> departments = departmentFetcher.List( Authority.Authority.GetUserProfile().SiteId,
                Lor.OrderBy.GetDefault(), Lor.Limit.GetDefault(), out message,  out rows,Lor.Restrain.Gt("ParentId", 0));
            bool recordBeRoot = e.Insert.ParentId == 0;
            bool rootInDb = departments.Count == 1;
            /*
             * 
             * 本次新增（修改）为Root           数据库有Root            是否允许写入
             * 是                               是                      否
             * 否                               否                      否
             * 是                               否                      是
             * 否                               是                      是
            */
        }
    }
}
